package com.ccx.coachsite.util;


import java.io.File;
import java.io.FileInputStream;

import android.content.ContentResolver;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.LinearGradient;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.PorterDuff.Mode;
import android.graphics.PorterDuffXfermode;
import android.graphics.Shader.TileMode;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.view.Display;

public class GraphicUtils {

	private static final String TAG = "GraphicUtils";
	
	
	/**
	 * 图片缩放（根据新的宽度与高度）
	 * @param bmp
	 * @param newWidth
	 * @param newHeiht
	 * @return
	 */
	public static Bitmap resizeImage(Bitmap bmp,int newWidth,int newHeiht)
	{
		if (bmp == null) {
			return null;
		}
		
		int originWidth=bmp.getWidth();
		int originHeight=bmp.getHeight();
		
		//计算缩放�?
		float scaleWidth=((float)newWidth)/originWidth;
		float scaleHeight=((float)newHeiht)/originHeight;
		
		//创建图片的Matrics对象
		Matrix matrix=new Matrix();
		
		//缩放动作
		matrix.postScale(scaleWidth, scaleHeight);
		
		//旋转图片
		//matrix.postRotate(45);
		
		//创建新图�?
		Bitmap resizeBitmap=Bitmap.createBitmap(bmp, 0, 0, originWidth, originHeight, matrix, true);
		
		return resizeBitmap;
		
	}
	
	/**
	 * 图片缩放（根据缩放比例）
	 * @param bmp
	 * @param scale
	 * @return
	 */
	public static Bitmap resizeImage(Bitmap bmp,float scale)
	{
		if (bmp == null) {
			return null;
		}
		
		int originWidth=bmp.getWidth();
		int originHeight=bmp.getHeight();
		
		
		//创建图片的Matrics对象
		Matrix matrix=new Matrix();
		
		//缩放动作
		matrix.postScale(scale, scale);
		
		//旋转图片
		//matrix.postRotate(45);
		
		//创建新图�?
		Bitmap resizeBitmap=Bitmap.createBitmap(bmp, 0, 0, originWidth, originHeight, matrix, true);
		
		return resizeBitmap;
		
	}
	
	/**
	 * 获取具有倒影的图片
	 * @param bitmap 
	 * @return
	 */
	public static Bitmap createReflectionImageWithOrigin(Bitmap bitmap){

		final int reflectionGap = 4;

		int width = bitmap.getWidth();

		int height = bitmap.getHeight();

		Matrix matrix = new Matrix();

		matrix.preScale(1, -1);

		Bitmap reflectionImage = Bitmap.createBitmap(bitmap,

		0, 320, width,height-320, matrix, false);
		

		Bitmap bitmapWithReflection = Bitmap.createBitmap(width, (height + height-320), Config.ARGB_8888);

		Canvas canvas = new Canvas(bitmapWithReflection);

		canvas.drawBitmap(bitmap, 0, 0, null);

		Paint deafalutPaint = new Paint();

		canvas.drawRect(0, height,width,height + reflectionGap,

		deafalutPaint);

		canvas.drawBitmap(reflectionImage, 0, height + reflectionGap, null);

		Paint paint = new Paint();
		LinearGradient shader = new LinearGradient(0,

				bitmap.getHeight(), 0, bitmapWithReflection.getHeight()

				, 0x90383636, 0xff383636, TileMode.MIRROR);

			paint.setShader(shader);

			// Set the Transfer mode to be porter duff and destination in

			paint.setXfermode(new PorterDuffXfermode(Mode.SRC_OVER));

			// Draw a rectangle using the paint with our linear gradient

			canvas.drawRect(0, height, width, bitmapWithReflection.getHeight()+ reflectionGap, paint);
			bitmap.recycle();
			reflectionImage.recycle();
				return bitmapWithReflection;
		

	}
	
	/**
	 * 获取图片文件，以屏幕大小为参考
	 * @param filePath
	 * @param display
	 * @return
	 */
	public static Bitmap getImageFile(String filePath,Display display)
	{
		File file=new File(filePath);
		if(!file.exists())//文件不存在
			return null;
		int sWidth=display.getWidth(); //屏宽
		int sHeight=display.getHeight();//屏高
		BitmapFactory.Options opts=new BitmapFactory.Options();
		opts.inJustDecodeBounds=true;
		//先测量图片的尺寸
		BitmapFactory.decodeFile(filePath, opts);
		int imWidth=opts.outWidth; //图片宽
		int imHeight=opts.outHeight; //图片高
		
		int scale=1;
		if(imWidth>imHeight)
			scale=Math.round((float)imWidth/sWidth);
		else
			scale=Math.round((float)imHeight/sHeight);
		scale=scale==0?1:scale;
		
		opts.inJustDecodeBounds=false;
		opts.inSampleSize=scale;
		FileInputStream fis=null;
		Bitmap tmpBmp=null;
		try {
			fis = new FileInputStream(new File(filePath));
			tmpBmp=BitmapFactory.decodeStream(fis, null, opts);
			fis.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return tmpBmp;
		
	}
	
	
	
	/**
	 * 获取图片文件，指定大小来获取，如果指定的大小超过原图尺寸，则按原图尺寸返回
	 * @param contentUri 图片资源
	 * @param ctx
	 * @param targetWidth
	 * @param targetHeight
	 * @return
	 * @throws Exception
	 */
	public static Bitmap getImageFile(Uri contentUri,Context ctx,int targetWidth,int targetHeight) throws Exception
	{
		if(contentUri==null)//文件不存在
			return null;
		ContentResolver cr=ctx.getContentResolver();
		BitmapFactory.Options opts=new BitmapFactory.Options();
		opts.inJustDecodeBounds=true;
		//先测量图片的尺寸
		if(contentUri.toString().indexOf("content")!=-1)
			BitmapFactory.decodeStream(cr.openInputStream(contentUri), null, opts);
		else
			BitmapFactory.decodeFile(contentUri.toString(), opts);
			
		int imWidth=opts.outWidth; //图片宽
		int imHeight=opts.outHeight; //图片高
		
		int scale=1;
		if(imWidth>imHeight)
			scale=Math.round((float)imWidth/targetWidth);
		else
			scale=Math.round((float)imHeight/targetHeight);
		scale=scale==0?1:scale;
		
		opts.inJustDecodeBounds=false;
		opts.inSampleSize=scale;
		Bitmap tmpBmp=null;
		if(contentUri.toString().indexOf("content")!=-1)
			tmpBmp=BitmapFactory.decodeStream(cr.openInputStream(contentUri), null, opts);
		else{
			FileInputStream fis=new FileInputStream(new File(contentUri.toString()));
			tmpBmp=BitmapFactory.decodeStream(fis, null, opts);
		}
		return tmpBmp;
		
	}
	
	/**
	 * 获取图片文件，指定大小来获取，如果指定的大小超过原图尺寸，则按原图尺寸返回
	 * 
	 * @param contentUri
	 *            图片资源
	 * @param ctx
	 * @param targetWidth
	 * @param targetHeight
	 * @return
	 * @throws Exception
	 */
	public static Drawable getDrawableImageFile(Uri contentUri, Context ctx, int targetWidth, int targetHeight) {
		try{
			return new BitmapDrawable(ctx.getResources(), getImageFile(contentUri, ctx, targetWidth, targetHeight));
		}catch(Exception e){
			e.printStackTrace();
			return null;
		}
	}
	
}
